import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 * 地表水体污染因子字典
 * guopeng
 * 2017.11.27
 */
export class WaterPollutant{
  constructor(
    private id : string,//污染物id
    private waterPollutantCode : string,//污染物编码
    private waterPollutantName : string,//污染物名称
    private waterOriginalPollutantCode : string,//污染物原编码
    private waterUnitConcentration :string,//单位
    private waterUnitDischarge : string,//排放量单位
    private waterTypeConcentration : string,//值格式
    private convertFlag : string,//折算标识0无折算值1有折算值
    private extensionInfo : string//拓展信息
  ){}
}

@Component({
  selector: 'app-waterPollutant',
  templateUrl: './waterPollutant.component.html',
  styleUrls: ['./waterPollutant.component.css']
})
export class WaterPollutantComponent implements OnInit{
  private waterPollutantCode : string;//污染物编码
  private waterPollutantName : string;//污染物名称
  errorMessage : string;
  private list:Array<any>;

  public queryMessage:WaterPollutant = new WaterPollutant("","","","","","","","","");
  public updateMessages:WaterPollutant = new WaterPollutant("","","","","","","","","");
  public addMessages:WaterPollutant =  new WaterPollutant("","","","","","","","","");
  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService) {

  }
  add(){
    this.addMessages =  new WaterPollutant("","","","","","","","","");
  }

  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let waterPollutantCode = this.waterPollutantCode== undefined ? '' : this.waterPollutantCode ;
    let waterPollutantName = this.waterPollutantName== undefined ? '' : this.waterPollutantName;
    let current = pageNo;
    this.http.get(this.url+'/waterPollutant/queryWaterPollutantListByPage?waterPollutantCode='+waterPollutantCode+'&waterPollutantName='+waterPollutantName+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }
  userMessage(event:any){
    let query = new WaterPollutant("","","","","","","","","");
    let update = new WaterPollutant("","","","","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let waterPollutantCode = info.waterPollutantCode== undefined ? '' : info.waterPollutantCode ;
    let waterPollutantName = info.waterPollutantName== undefined ? '' : info.waterPollutantName;
    let current = this.curPage;
    this.http.get(this.url+'/waterPollutant/queryWaterPollutantListByPage?waterPollutantCode='+waterPollutantCode+'&waterPollutantName='+waterPollutantName).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/waterPollutant/addWaterPollutant',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101117001'){
          this.errorMessage = "地表水体污染源因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117002'){
          this.errorMessage = "地表水体污染源因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117003'){
          this.errorMessage = "地表水体污染源因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117004'){
          this.errorMessage = "地表水体污染源因子名称已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117005'){
          this.errorMessage = "地表水体污染源因子折算标识为空！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/waterPollutant/deleteWaterPollutant/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101117006'){
        this.errorMessage = "该地表水体污染源因子不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/waterPollutant/updateWaterPollutant',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101117001'){
          this.errorMessage = "地表水体污染源因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117002'){
          this.errorMessage = "地表水体污染源因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117003'){
          this.errorMessage = "地表水体污染源因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117004'){
          this.errorMessage = "地表水体污染源因子名称已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117005'){
          this.errorMessage = "地表水体污染源因子折算标识为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101117006'){
          this.errorMessage = "该地表水体污染源因子不存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/waterPollutant/queryWaterPollutantListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}




